﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXQ.Dao.ViewModel;
using AutoMapper;
namespace YXQ.Dao
{

    public class PayDao
    {


        public PayDao()
        {

            Mapper.CreateMap<Y_Pay, PayModel>();
        }
        public List<Y_Pay> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                recordcount = edm.Y_Pay.Count();

                return edm.Y_Pay.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public PayModel GetOneById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_Pay.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<Y_Pay, PayModel>(query);
            }

        }


        public int Save(PayModel obj)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_Pay.FirstOrDefault(s => s.Id == obj.id);

                if (query == null)
                { 
                    query = new Y_Pay();
                    query.createtime = obj.createtime;
                    query.createuserid = obj.createuserid;
                    query.createusername = obj.createusername;
                    edm.AddToY_Pay(query); 
                }


                query.ProjectId = obj.ProjectId;

                query.paymoney = obj.paymoney;

                query.remark = obj.remark;

                query.paydate = obj.paydate;

                query.paytype = obj.paytype;

                query.mtype = obj.mtype;

                

                edm.SaveChanges();

                UpdateCw(obj);


                return query.Id;


            }

        }

        private void UpdateCw(PayModel pmdl)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            { 
                var query= edm.Y_Pay.Where(s=>s.ProjectId==pmdl.ProjectId&&s.paytype==pmdl.paytype&&s.mtype==pmdl.mtype);
                if (query.Count() > 0) {
                    var sum = query.ToList().Sum(s=>s.paymoney);
                    var cwmodel = edm.Y_CW.OrderByDescending(s=>s.Id).FirstOrDefault(s=>s.ProjectId==pmdl.ProjectId);
                    if (cwmodel == null) {
                        cwmodel = new Y_CW();
                        cwmodel.ProjectId = pmdl.ProjectId;
                        cwmodel.createrealname = pmdl.createrealname;
                        cwmodel.createusername = pmdl.createusername;
                        cwmodel.createtime = pmdl.createtime;
                        cwmodel.createuserid = pmdl.createuserid;

                        edm.AddToY_CW(cwmodel);
                    }

                    switch(pmdl.paytype){
                        case 1:
                            if (pmdl.mtype == 1)
                            {
                                cwmodel.gcyf = sum;
                            }
                            else {
                                cwmodel.gcyt = sum;
                            }
                            break;
                        case 2:
                            if (pmdl.mtype == 1)
                            {
                                cwmodel.sjyf = sum;
                            }
                            else
                            {
                                cwmodel.sjyt = sum;
                            }
                            break;
                        case 3:
                            if (pmdl.mtype == 1)
                            {
                                cwmodel.jlyf = sum;
                            }
                            if (pmdl.mtype == 2)
                            {
                                cwmodel.jlyt = sum;
                            }
                            if (pmdl.mtype == 3)
                            {
                                cwmodel.jlzj = sum;
                            }
                            
                            break;
                        case 4: 
                             cwmodel.kcyf = sum;
                            
                            break;
                        case 5: 
                             cwmodel.gsyf = sum; 
                            break;
                        case 6: 
                            cwmodel.zjyf = sum;
                            
                            break;
                        case 7: 
                             cwmodel.dlyf = sum; 
                            break;
                        case 8: 
                            cwmodel.cgyf = sum; 
                            break;
                        case 9: 
                            cwmodel.qtyf = sum;
                            break;
                    }
                    edm.SaveChanges();
                }
            }
            
        }
        public List<PayModel> GetModel(string remark,string dtstart, string dtend,int proid,int mtype,int ptype, int startIndex, int maxrow, out int record)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = from s in edm.Y_Pay select s;

                if (!string.IsNullOrEmpty(remark)) {
                    query = query.Where(s=>s.remark.IndexOf(remark)>=0);
                }
                if (!string.IsNullOrEmpty(dtstart)) {
                    var dt = Convert.ToDateTime(dtstart);
                    query = query.Where(s=>s.paydate>=dt);
                }


                if (!string.IsNullOrEmpty(dtend)) {
                    var dts = Convert.ToDateTime(dtend);
                    query = query.Where(s=>s.paydate<=dts);
                }
                if (proid > 0) {
                    query = query.Where(s=>s.ProjectId==proid);
                }

                if (mtype > 0) {
                    query = query.Where(s=>s.mtype==mtype);
                }
                if (ptype > 0) {
                    query = query.Where(s=>s.paytype==ptype);
                }

                record = query.Count();
                return Mapper.Map<List<Y_Pay>, List<PayModel>>(query.OrderByDescending(s=>s.Id).Skip(startIndex).Take(maxrow).ToList());
            }
        }

        public int DelById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_Pay.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {
                    DelCwMoney(query);

                    edm.DeleteObject(query); 
                } 
                return edm.SaveChanges();


            }

        }

        private void DelCwMoney(Y_Pay pmdl)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var cwmodel = edm.Y_CW.FirstOrDefault(s => s.ProjectId == pmdl.ProjectId);
                if (cwmodel != null)
                { 

                    switch (pmdl.paytype)
                    {
                        case 1:
                            if (pmdl.mtype == 1)
                            {
                                cwmodel.gcyf -= pmdl.paymoney;
                            }
                            else
                            {
                                cwmodel.gcyt -= pmdl.paymoney;
                            }
                            break;
                        case 2:
                            if (pmdl.mtype == 1)
                            {
                                cwmodel.sjyf -= pmdl.paymoney;
                            }
                            else
                            {
                                cwmodel.sjyt -= pmdl.paymoney;
                            }
                            break;
                        case 3:
                            if (pmdl.mtype == 1)
                            {
                                cwmodel.jlyf -= pmdl.paymoney;
                            }
                            if (pmdl.mtype == 2)
                            {
                                cwmodel.jlyt -= pmdl.paymoney;
                            }
                            if (pmdl.mtype == 3)
                            {
                                cwmodel.jlzj -= pmdl.paymoney;
                            }

                            break;
                        case 4:
                            cwmodel.kcyf -= pmdl.paymoney;

                            break;
                        case 5:
                            cwmodel.gsyf -= pmdl.paymoney;
                            break;
                        case 6:
                            cwmodel.zjyf -= pmdl.paymoney;

                            break;
                        case 7:
                            cwmodel.dlyf -= pmdl.paymoney;
                            break;
                        case 8:
                            cwmodel.cgyf -= pmdl.paymoney;
                            break;
                        case 9:
                            cwmodel.qtyf -= pmdl.paymoney;
                            break;
                    }
                    edm.SaveChanges();
                }
            }
        }


    }

}

